package com.itheima.controller;


import com.itheima.DTO.Login;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/member")
@Slf4j
@Api(tags = "用户接口")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @PostMapping("/login")
    @ApiOperation("登录")
    public Result login(@RequestBody Login loginDTO) {
        log.info("用户登录：{}", loginDTO);
        String message = "";
        try {
            message = memberService.login(loginDTO);
            if (message == null || !message.equals(MessageConstant.LOGIN_SUCCESS)){
                log.info("用户登录失败");
                return new Result(false, message);
            }else{
                log.info("用户登录成功");
                return new Result(true, MessageConstant.LOGIN_SUCCESS);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }


    }
}
